package com.kehao.newcoder;

import com.kehao.utils.LeetCodeUtil;
import org.junit.Test;

import java.util.ArrayList;

public class NC38Solution {
    public ArrayList<Integer> spiralOrder(int[][] matrix) {
        ArrayList<Integer> result = new ArrayList<>();
        if(matrix.length==0) return result;
        int up = 0, down = matrix.length - 1, left = 0, right = matrix[0].length - 1;
        int direction = 0;
        int pi = 0,pj = 0;
        while (up <= down && left <= right) {
            switch (direction) {
                case 0:
                    for (pj = left; pj <= right; pj++) {
                       result.add(matrix[pi][pj]);
                    }
                    up ++;
                    pj --;
                    break;
                case 1:
                    for (pi = up; pi <= down; pi++) {
                        result.add(matrix[pi][pj]);
                    }
                    right --;
                    pi--;
                    break;
                case 2:
                    for (pj = right; pj >=left ; pj--) {
                        result.add(matrix[pi][pj]);
                    }
                    down--;
                    pj++;
                    break;
                case 3:
                    for (pi = down; pi >=up ; pi--) {
                        result.add(matrix[pi][pj]);
                    }
                    left++;
                    pi++;
                    break;
            }
            direction = (direction +1)%4;
        }
        return result;
    }

    @Test
    public void getArray(){
        String s = LeetCodeUtil.bracketTransform("[[1,2,3],[4,5,6],[7,8,9]]");
        System.out.println(s);
    }


    @Test
    public void test(){
        ArrayList<Integer> result = spiralOrder(new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
        for (int i = 0; i < result.size(); i++) {
            System.out.print(result.get(i)+" ");
        }
    }
}
